package com.eims.sls_app.ui;

import java.text.DecimalFormat;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.RequestParams;

import bls.juejin.cn.R;

import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.ui.ConfirmselectionActivity.MyHandler;
import com.eims.sls_app.ui.ConfirmselectionActivity.PayAsynTask;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.BaseHelper;
import com.eims.sls_app.utils.RequestUtil;
import com.eims.sls_app.view.MyToast;

import org.json.JSONObject;

public class JoinActivity extends MyBaseActivity implements OnClickListener {
	private Context context = JoinActivity.this;
	private final static String TAG = "JoinActivity";
	private ImageButton btn_back; // 返回
	private ImageView iv_up_down;// 玩法提示图标
	private Button btn_playtype;// 玩法
	private TextView follow_total_money, follow_total_copy; // 方案金额// 多少份
	// 每份金额
	private Button follow_btn_money1, follow_btn_money2;// 1元，2元
	private EditText follow_et_yj;// 中奖佣金
	private Button follow_btn_sub_yj;// 减
	private Button follow_btn_add_yj;// 加
	private Button follow_btn_sub_wantbuy;// 减
	private Button follow_btn_add_wantbuy;// 加
	private Button follow_btn_sub_bd;// 减
	private Button follow_btn_add_bd;// 加
	private EditText follow_et_wantbuy, follow_et_bd; // 购买份数// 保底份数
	private EditText follow_et_name, follow_et_description; // 方案标题// 方案描述
	// 密码设置
	private Button follow_btn_public, follow_btn_toend, follow_btn_afterwin, follow_btn_baomi;
	private TextView follow_tv_total_money; // 应付金额
	private Button btn_submit;
	private ImageButton btn_help;
	private long totalCount; // 一共有多少份
	private int totalMoney; // 总金额
	private double shareMoney = 1; // 每份金额
	private long payMoney = 0, count = 0; // 应付金额// 购买的份数
	private int Bonus = 0; // 佣金
	private int secrecyLevel = 0; // 保密设置
	private int baoCount = 0; // 保底份数
	private boolean flag = false; // 是否追号。
	private long fen; // 至少要买的份数
	private String messgae;
	private RequestUtil requestUtil;
	private int a = 0;// 发起合买的类型。1普通。2.竞足优化合买。3.竞蓝优化合买
	private DecimalFormat format = new DecimalFormat("#####0.00");
	private String yjjj;

	public String opt, auth, info, time, imei, crc; // 格式化后的参数
	private MyHandler mHandler;
	private ProgressDialog pd;
	private String simId, stoptime;

	String SucJinECost, SucCaiJinCost, SuccMoney;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_join);
		App.activityS1.add(this);
		mHandler = new MyHandler();
		findView();
		setListener();
		init();
		getScaleParams();
	}

	/**
	 * 初始化UI
	 */
	private void findView() {
		follow_total_money = (TextView) this.findViewById(R.id.follow_total_money);
		follow_total_copy = (TextView) this.findViewById(R.id.follow_total_copy);
		follow_btn_money1 = (Button) this.findViewById(R.id.follow_btn_money1);
		follow_btn_money2 = (Button) this.findViewById(R.id.follow_btn_money2);
		btn_back = (ImageButton) this.findViewById(R.id.btn_back);
		iv_up_down = (ImageView) this.findViewById(R.id.iv_up_down);
		btn_playtype = (Button) this.findViewById(R.id.btn_playtype);
		follow_et_yj = (EditText) this.findViewById(R.id.follow_et_yj);
		follow_btn_sub_yj = (Button) this.findViewById(R.id.follow_btn_sub_yj);
		follow_btn_add_yj = (Button) this.findViewById(R.id.follow_btn_add_yj);
		follow_btn_sub_wantbuy = (Button) this.findViewById(R.id.follow_btn_sub_wantbuy);
		follow_btn_add_wantbuy = (Button) this.findViewById(R.id.follow_btn_add_wantbuy);
		follow_btn_sub_bd = (Button) this.findViewById(R.id.follow_btn_sub_bd);
		follow_btn_add_bd = (Button) this.findViewById(R.id.follow_btn_add_bd);
		follow_et_wantbuy = (EditText) this.findViewById(R.id.follow_et_wantbuy);
		follow_et_bd = (EditText) this.findViewById(R.id.follow_et_bd);
		follow_btn_public = (Button) this.findViewById(R.id.follow_btn_public);// 公开
		follow_btn_baomi = (Button) this.findViewById(R.id.follow_btn_baomi);// 永久保密
		follow_btn_toend = (Button) this.findViewById(R.id.follow_btn_toend);// 跟单可见
		follow_btn_afterwin = (Button) this.findViewById(R.id.follow_btn_afterwin);// 到开奖

		follow_et_name = (EditText) this.findViewById(R.id.follow_et_name);
		// follow_et_description = (EditText)
		// this.findViewById(R.id.follow_et_description);

		follow_tv_total_money = (TextView) this.findViewById(R.id.follow_tv_total_money);

		btn_submit = (Button) this.findViewById(R.id.btn_submit);
		btn_help = (ImageButton) this.findViewById(R.id.btn_help);
	}

	/**
	 * 初始化属性
	 */
	private void init() {
		Intent intent = getIntent();
		if (null != intent) {
			String t = intent.getStringExtra("totalMoney");
			a = intent.getIntExtra("join_type", 0);

			totalMoney = Integer.parseInt(t);
			if (a == 0) {
				yjjj = intent.getStringExtra("yjjj");
				stoptime = intent.getStringExtra("stoptime");
				Log.e("xxx", yjjj + "," + stoptime);
			} else if (a == 1) {
				yjjj = intent.getStringExtra("yjjj");
				stoptime = intent.getStringExtra("stoptime");
				Log.e("xxx", yjjj + "," + stoptime);
			}

			if (intent.getStringExtra("flag") != null) {
				this.flag = true;
			}
		}
		follow_total_money.setText(totalMoney + "");
		follow_total_copy.setText(totalMoney + "");
		totalCount = totalMoney;
		fen = 1;
		if (!"".equals(App.followLeastBuyScale)) {
			fen = (int) Math.ceil(totalCount * Double.parseDouble(App.followLeastBuyScale));
		}
		int mLowBounds = 0;
		if (!"".equals(App.followCommissionScale) && App.followCommissionScale.contains(".")) {
			String commission = App.followCommissionScale;
			String num = commission.substring(commission.indexOf(".") + 1);
			mLowBounds = Integer.parseInt(num);
		}
		shareMoney = 1.0;
		follow_et_yj.setText(mLowBounds + "");
		count = fen;
		follow_et_wantbuy.setText(fen + "");
		setTextChange();
		iv_up_down.setVisibility(View.GONE);
		btn_playtype.setText("发起合买");
	}

	/**
	 * 设置监听
	 */
	private void setListener() {
		follow_btn_money1.setOnClickListener(this);
		follow_btn_money2.setOnClickListener(this);
		btn_back.setOnClickListener(this);
		follow_btn_public.setOnClickListener(this);
		follow_btn_baomi.setOnClickListener(this);
		follow_btn_toend.setOnClickListener(this);
		follow_btn_afterwin.setOnClickListener(this);
		btn_submit.setOnClickListener(this);
		btn_help.setOnClickListener(this);
		follow_et_wantbuy.addTextChangedListener(watcher_wantbuy);
		follow_btn_sub_wantbuy.setOnClickListener(this);
		follow_btn_add_wantbuy.setOnClickListener(this);
		follow_et_bd.addTextChangedListener(watcher_bd);
		follow_btn_sub_bd.setOnClickListener(this);
		follow_btn_add_bd.setOnClickListener(this);
		follow_et_yj.addTextChangedListener(yj_textWatcher);
		follow_btn_sub_yj.setOnClickListener(this);
		follow_btn_add_yj.setOnClickListener(this);
	}

	public void changEditText(EditText et, int type) {
		if (null != et) {
			String content = et.getText().toString();
			int number = Integer.parseInt(content);
			if (1 == type) {// 减
				number -= 1;
			} else if (2 == type) {// 加
				number += 1;
			}
			et.setText(number + "");
		}
	}

	/**
	 * 当文本的值改变时
	 */
	private TextWatcher yj_textWatcher = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		}

		@Override
		public void afterTextChanged(Editable edt) {
			int bounds = 0;
			follow_et_yj.setHint("默认为6");
			if ("".equals(edt.toString())) {// 为空
				// follow_et_yj.setText("0");// 最高佣金为10%
				bounds = 0;
			} else {
				if (Integer.parseInt(edt.toString().trim()) > 10) {
					bounds = 10;
					follow_et_yj.setText(10 + "");// 最高佣金为10%
					MyToast.getToast(JoinActivity.this, "最高佣金为10%").show();
				} else if (Integer.parseInt(edt.toString().trim()) < 0) {// 低于最低佣金
					bounds = 0;
					follow_et_yj.setText(0 + "");// 最低佣金
				} else {
					bounds = Integer.parseInt(edt.toString().trim());
				}
			}
			Bonus = bounds;
			setCursorPosition(follow_et_yj);
		}
	};

	public void setCursorPosition(EditText et) {
		CharSequence text = et.getText();
		if (text != null) {
			Selection.setSelection((Spannable) text, text.length());
		}
	}

	/**
	 * 文本框文本 监听
	 */
	private TextWatcher watcher_wantbuy = new TextWatcher() {
		@Override
		public void afterTextChanged(Editable s) {
			int max = (Integer.parseInt(follow_total_copy.getText().toString()));
			follow_et_wantbuy.setHint("最低" + fen + "份");
			if ("".equals(s.toString())) {// 为空
				count = 0;
				// count = fen;
				Log.i(TAG, "最低份数---" + fen);
				// follow_et_wantbuy.setText(fen + "");
				follow_et_wantbuy.setSelection(follow_et_wantbuy.getText().length());
			} else {
				if (Integer.parseInt(s.toString()) > max) {
					MyToast.getToast(JoinActivity.this, "最多只能买" + max + "份.").show();
					count = max;
					follow_et_wantbuy.setText(max + "");
					follow_et_wantbuy.setSelection(follow_et_wantbuy.getText().length());
				} else if (Integer.parseInt(s.toString()) < fen) {
					// MyToast.getToast(JoinActivity.this, "最少买" + fen + "份.")
					// .show();
					// count = fen;
					count = Integer.parseInt(s.toString());
					// follow_et_wantbuy.setText(fen + "");
					follow_et_wantbuy.setSelection(follow_et_wantbuy.getText().length());
				} else {
					count = Integer.parseInt(s.toString());
				}
			}
			follow_et_bd.setText("0");
			baoCount = 0;
			setTextChange();
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}
	};

	/**
	 * 文本框文本 监听
	 */
	private TextWatcher watcher_bd = new TextWatcher() {
		@Override
		public void afterTextChanged(Editable s) {
			if ((s.toString().trim()).length() == 0) {
				baoCount = 0;
				return;
			}
			int buyCount = 0;
			if (!"".equals(follow_et_wantbuy.getText().toString().trim())) {
				buyCount = Integer.parseInt(follow_et_wantbuy.getText().toString().trim());
			}
			int max = (int) (Integer.parseInt(follow_total_copy.getText().toString()) - buyCount);
			if (Integer.parseInt(s.toString()) > max) {
				if (0 == max) {
					baoCount = 0;
					follow_et_bd.setText("0");
					follow_et_bd.setSelection(follow_et_bd.getText().length());
					MyToast.getToast(JoinActivity.this, "没有可保底份数").show();
				} else {
					baoCount = max;
					follow_et_bd.setText(max + "");
					MyToast.getToast(JoinActivity.this, "最多只能保底" + max + "份").show();
					follow_et_bd.setSelection(follow_et_bd.getText().length());
				}
			} else if (Integer.parseInt(s.toString()) < 0) {
				follow_et_bd.setText("0");
				baoCount = 0;
				follow_et_bd.setSelection(follow_et_bd.getText().length());
			}
			setTextChange();
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}
	};

	/**
	 * 选出每份 的金额
	 */
	public void setShareMoney(Button btn) {
		follow_btn_money1.setBackgroundResource(R.drawable.follow_btn_bg);
		follow_btn_money1.setTextColor(Color.BLACK);
		follow_btn_money1.setClickable(true);
		follow_btn_money2.setBackgroundResource(R.drawable.follow_btn_bg);
		follow_btn_money2.setTextColor(Color.BLACK);
		follow_btn_money2.setClickable(true);
		btn.setBackgroundResource(R.color.bet_btn_text);
		btn.setTextColor(Color.WHITE);
		btn.setClickable(false);
	}

	/**
	 * 设置投注保密性
	 */
	public void setPass(Button btn, int level) {
		secrecyLevel = level;
		follow_btn_public.setBackgroundResource(R.drawable.follow_btn_bg);
		follow_btn_public.setTextColor(Color.BLACK);
		follow_btn_baomi.setBackgroundResource(R.drawable.follow_btn_bg);
		follow_btn_baomi.setTextColor(Color.BLACK);
		follow_btn_toend.setBackgroundResource(R.drawable.follow_btn_bg);
		follow_btn_toend.setTextColor(Color.BLACK);
		follow_btn_afterwin.setBackgroundResource(R.drawable.follow_btn_bg);
		follow_btn_afterwin.setTextColor(Color.BLACK);
		btn.setBackgroundResource(R.color.bet_btn_text);
		btn.setTextColor(Color.WHITE);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		btn_submit.setEnabled(true);
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 每份金额
		case R.id.follow_btn_money1:
			follow_et_bd.setText("0");
			shareMoney = 1.0;
			setShareMoney(follow_btn_money1);
			totalCount = totalMoney;
			follow_total_copy.setText(totalCount + "");
			setTextChange();
			fen = (int) Math.ceil(Integer.parseInt(follow_total_copy.getText().toString().trim())
					* Double.parseDouble(App.followLeastBuyScale));
			follow_et_wantbuy.setText(fen + "");
			break;
		case R.id.follow_btn_money2:
			follow_et_bd.setText("0");
			shareMoney = 2.0;
			setShareMoney(follow_btn_money2);
			totalCount = totalMoney / 2;
			follow_total_copy.setText(totalCount + "");
			setTextChange();
			fen = (int) Math.ceil(Integer.parseInt(follow_total_copy.getText().toString().trim())
					* Double.parseDouble(App.followLeastBuyScale));
			follow_et_wantbuy.setText(fen + "");
			break;
		// // 佣金比
		case R.id.follow_btn_sub_yj:
			// changEditText(follow_et_yj, 1);// 减
			// break;
			if (!"0".equals(follow_et_yj.getText().toString().trim())
					&& !"".equals(follow_et_yj.getText().toString().trim())) {
				changEditText(follow_et_yj, 1);// 减 为0或""时不能减
				setTextChange();
			}
			break;
		case R.id.follow_btn_add_yj:
			if ("".equals(follow_et_yj.getText().toString().trim())) {
				follow_et_yj.setText("1");
			} else {
				changEditText(follow_et_yj, 2);// 加 为""时点击+将et设置为1
			}
			break;
		// // 购买份数
		case R.id.follow_btn_sub_wantbuy:
			// TODO follow_et_wantbuy
			if ((fen + "").equals(follow_et_wantbuy.getText().toString().trim())) {
				MyToast.getToast(JoinActivity.this, "最少购买" + fen + "份!").show();
				return;
			}
			if (!"0".equals(follow_et_wantbuy.getText().toString().trim())
					&& !"".equals(follow_et_wantbuy.getText().toString().trim())) {
				changEditText(follow_et_wantbuy, 1);// 减
				setTextChange();
			}
			break;
		case R.id.follow_btn_add_wantbuy:
			if ("".equals(follow_et_wantbuy.getText().toString().trim())) {
				follow_et_wantbuy.setText("1");
			} else {
				changEditText(follow_et_wantbuy, 2);// 加 为""时点击+将et设置为1
				setTextChange();
			}
			break;
		// changEditText(follow_et_wantbuy, 2);// 加
		// setTextChange();
		// break;
		// // 保底
		case R.id.follow_btn_sub_bd:
			if (!"".equals(follow_et_bd.getText().toString().trim())) {
				changEditText(follow_et_bd, 1);// 减
				setTextChange();
			}
			break;
		// changEditText(follow_et_bd, 1);// 减
		// break;
		case R.id.follow_btn_add_bd:
			if ("".equals(follow_et_bd.getText().toString().trim())) {
				follow_et_bd.setText("1");
			} else {
				changEditText(follow_et_bd, 2);// 加 为""时点击+将et设置为1
				setTextChange();
			}
			break;
		// changEditText(follow_et_bd, 2);// 加
		// break;
		// 投注保密设置
		case R.id.follow_btn_public:
			setPass(follow_btn_public, 0);
			break;
		case R.id.follow_btn_toend:
			setPass(follow_btn_toend, 1);
			break;
		case R.id.follow_btn_afterwin:
			setPass(follow_btn_afterwin, 2);
			break;
		case R.id.follow_btn_baomi:
			setPass(follow_btn_baomi, 3);
			break;
		// 付款
		case R.id.btn_submit:
			Log.i(TAG, "选了多少份" + count);
			if (count < fen) {
				Toast.makeText(this, "至少购买" + fen + "份", Toast.LENGTH_SHORT).show();
			} else {
				btn_submit.setEnabled(false);

				commit();

			}
			break;
		// 帮助
		case R.id.btn_help:
			Intent intent = new Intent(JoinActivity.this, JoinHelpActivity.class);
			JoinActivity.this.startActivity(intent);
			break;
		case R.id.btn_back:
			JoinActivity.this.finish();
			break;
		}
	}

	// 时间是检验能力的唯一标准，大家一起来跟单
	public void commit() {
		String mTitle = follow_et_name.getText().toString();
		if (mTitle.equals("")) {
			mTitle = "时间是检验能力的唯一标准。";
		}
		// String mContent = follow_et_description.getText().toString().trim();
		String mContent = "联盈助手合买单";
		String bao = follow_et_bd.getText().toString().trim();
		if (bao.length() > 0)
			baoCount = Integer.parseInt(bao);

		if (App.user != null) {
			// setEnabled(false);
			requestUtil = new RequestUtil(context, false, 0, false, "正在支付...") {
				@Override
				public void responseCallback(JSONObject object) {
					// setEnabled(true);
					if (RequestUtil.DEBUG)
						Log.e(TAG, "合买支付彩结果" + object);
					btn_submit.setEnabled(true);
					if (null != object) {
						String error = object.optString("error");
						if ("0".equals(error)) {
							App.user.setBalance(object.optDouble("balance"));
							App.user.setFreeze(object.optDouble("freeze"));
							App.schemeId = object.optInt("schemeids");
							simId = object.optInt("schemeids") + "";
							SucJinECost = format.format(object.optDouble("currentMoeny")); // 金额消费
							Log.e("金额消费", SucJinECost);
							SucCaiJinCost = format.format(object.optDouble("currentHandsel")); // 彩金消费
							Log.e("彩金消费", SucCaiJinCost);
							SuccMoney = format.format(object.optDouble("handselMoney")); // 剩余彩金
							Log.e("剩余彩金", SuccMoney);
							for (int i = 0; i < App.activityS1.size(); i++) {
								App.activityS1.get(i).finish();
							}
							if (null != App.list_numbers)
								App.list_numbers.clear();
							App.totalCount = 0;
							clearAllLotterySelectData();
							if (a == 0) {
								mHandler.sendEmptyMessage(0);
							} else if (a == 1) {
								mHandler.sendEmptyMessage(0);
							} else {
								Intent intent = new Intent(getApplicationContext(), PaySuccessActivity.class);

								intent.putExtra("paymoney", payMoney);
								intent.putExtra("currentMoeny", SucJinECost);
								intent.putExtra("currentHandsel", SucCaiJinCost);
								intent.putExtra("handselMoney", SuccMoney);
								intent.putExtra("isJoin", false);
								JoinActivity.this.startActivity(intent);

							}

						} else if ("-500".equals(error)) {
							btn_submit.setEnabled(true);
							MyToast.getToast(context, "连接超时，请重试。").show();
						} else if ("10000".equals(error)) {
							btn_submit.setEnabled(true);
							MyToast.getToast(context, "支付失败，请重试。").show();
							init();
						} else if (error.equals("-110")) {
							Toast.makeText(JoinActivity.this, "订单处理中，请勿重复下单！", Toast.LENGTH_SHORT).show();
						} else {
							btn_submit.setEnabled(true);
							messgae = object.optString("msg");
							if (messgae.contains("info")) {
								MyToast.getToast(context, messgae + "！请检查是否包含非法字符：英文双引号\"\"!").show();
							} else {
								MyToast.getToast(context, messgae).show();
							}
							if (RequestUtil.DEBUG)
								Log.e(TAG, "支付出错," + messgae);
						}
					} else {
						btn_submit.setEnabled(true);
						MyToast.getToast(context, "支付出错").show();
						if (RequestUtil.DEBUG)
							Log.e(TAG, "支付出错");
					}
				}

				@Override
				public void responseError(VolleyError error) {
					MyToast.getToast(context, "支付出错").show();
					btn_submit.setEnabled(true);
					if (RequestUtil.DEBUG)
						Log.e(TAG, "支付出错," + error.getMessage());
				}
			};
			if (a == 0||a == 8||a == 9) {

				requestUtil.commitFollow(totalMoney, shareMoney, count, baoCount, Bonus, mTitle, mContent, secrecyLevel,
						flag);

			} else if (a == 1) {
				requestUtil.commitBetting_jc_voptimization(totalMoney, 1, BonusActivity.list_Show,
						BonusActivity.youhua_type, mTitle, mContent, baoCount, (int) (totalMoney / shareMoney),
						(int) count, secrecyLevel, Bonus);

				Log.e(TAG, "totalMoney=" + totalMoney + ",baoCount=" + baoCount + ",(int) (totalMoney / shareMoney)"
						+ (int) (totalMoney / shareMoney) + ",count=" + count+",secrecyLevel="+secrecyLevel+",Bonus"+Bonus);

			} else if (a == 2) {
				requestUtil.commitBetting_jc_voptimization(totalMoney, 1, jclq_voptimization_Activity.list_Show,
						BonusActivity.youhua_type, mTitle, mContent, baoCount, (int) (totalMoney / shareMoney),
						(int) count, secrecyLevel, Bonus);
			}
		} else {// 未登陆
			btn_submit.setEnabled(true);
			MyToast.getToast(JoinActivity.this, "请先登陆").show();
			Intent intent = new Intent(JoinActivity.this, LoginActivity.class);
			intent.putExtra("loginType", "join");
			JoinActivity.this.startActivity(intent);
		}
	}

	/**
	 * 获取合买佣金比列和合买最少购买比列
	 */
	public void getScaleParams() {
		requestUtil = new RequestUtil(context, false, 0) {
			@Override
			public void responseCallback(JSONObject jsonObject) {
				if (RequestUtil.DEBUG)
					Log.i(TAG, "获取合买佣金比列和合买最少购买比列jsonObject--- " + jsonObject.toString());
				if ("0".equals(jsonObject.optString("error"))) {
					App.followCommissionScale = jsonObject.optString("yongjin"); // 合买佣金比列
					App.followLeastBuyScale = jsonObject.optString("rengou"); // 合买最少购买比列
					init();
				}
			}

			@Override
			public void responseError(VolleyError error) {
				if (RequestUtil.DEBUG)
					Log.e(TAG, "获取合买佣金比列和合买最少购买比列出错--- " + error.getMessage());
				init();
			}
		};
		requestUtil.getBuyParams();
	}

	/**
	 * 更改显示文本 *
	 */
	private void setTextChange() {
		Log.i(TAG, "改变了文本显示---");
		int num = 0; // 购买个数
		if (!"".equals(follow_et_wantbuy.getText().toString().trim())) {
			num = Integer.parseInt(follow_et_wantbuy.getText().toString().trim());
		}
		payMoney = (long) (num * shareMoney);
		// payMoney = (long) (Integer.parseInt(follow_et_wantbuy.getText()
		// .toString().trim()) * shareMoney);
		// if (payMoney == 0)
		// return;
		Log.i(TAG, "payMoney---" + payMoney);
		follow_tv_total_money.setText(payMoney + "");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		this.finish();
	}

	/**
	 * 清除所有彩种所选数据
	 */
	public static void clearAllLotterySelectData() {
		Buy_DLT_Activit.clearHashSet();
		Buy_RX9_Activit.clearHashSet();
		Buy_SFC_Activity.clearHashSet();
		Select_jczqActivity.clearHashSet();
		Select_QlcActivity.clearHashSet();
		SelectNumberActivity.clearHashSet();
		SelectNumberActivityFC3D.clearHashSet();
		SelectNumberActivityPL3.clearHashSet();
		SelectNumberActivityPL5_QXC.clearHashSet();
	}

	@SuppressLint("HandlerLeak")
	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			if (pd != null) {
				pd.dismiss();
			}

			btn_submit.setEnabled(true);
			switch (msg.what) {
			case 0:
				pd = BaseHelper.showProgress(JoinActivity.this, "", "出票中...", true, false);
				PayAsynTask payAsynTask = new PayAsynTask();
				payAsynTask.execute();

				break;
			case 1:

				Intent intent = new Intent(getApplicationContext(), PaySuccessActivity.class);

				intent.putExtra("paymoney", payMoney);
				intent.putExtra("currentMoeny", SucJinECost);
				intent.putExtra("currentHandsel", SucCaiJinCost);
				intent.putExtra("handselMoney", SuccMoney);
				intent.putExtra("isJoin", false);
				JoinActivity.this.startActivity(intent);

				break;
			case -2:

				break;
			case -500:

				MyToast.getToast(JoinActivity.this, "连接超时").show();
				break;
			case -111:

				break;
			default:

				Toast.makeText(JoinActivity.this, "网络异常，购买失败。请重新点击付款购买。", Toast.LENGTH_SHORT).show();
				break;
			}
			super.handleMessage(msg);
		}
	}

	class PayAsynTask extends AsyncTask<Integer, Integer, String> {

		String error = "-1";
		String moneyYjjj = payMoney * Double.parseDouble(yjjj) + "";

		@Override
		protected String doInBackground(Integer... params) {

			opt = "85";
			time = RspBodyBaseBean.getTime();
			imei = RspBodyBaseBean.getIMEI(JoinActivity.this);

			info = RspBodyBaseBean.change_TheSun2(simId, "", (totalMoney) * Double.parseDouble(yjjj) + "", stoptime,
					"0");

			String key = App.key;
			crc = RspBodyBaseBean.getCrc(time, imei, key, info, App.user.getUid());
			auth = RspBodyBaseBean.getAuth(crc, time, imei, App.user.getUid());
			String[] values = { opt, auth, info };
			Log.i(TAG, "晒单" + RequestParams.convertParams(context, opt, info));
			String result = HttpUtils.doPost(App.names, values, App.path);
			System.out.println("晒单：" + result);
			if (result.equals("success")) {
				error = "1";
			} else {
				error = "-1";
			}

			return error;
		}

		@Override
		protected void onPostExecute(String result) {
			mHandler.sendEmptyMessage(Integer.parseInt(result));
			super.onPostExecute(result);
		}
	}
}
